package com.erp.erp_project.controller;

import com.erp.erp_project.core.AjaxResult;
import com.erp.erp_project.core.annotation.Permission;
import com.erp.erp_project.mapper.DispatchMapper;
import com.erp.erp_project.mapper.EquipMapper;
import com.erp.erp_project.mapper.PlanMapper;
import com.erp.erp_project.mapper.WorkMapper;
import com.erp.erp_project.model.Dispatch;
import com.erp.erp_project.model.Equip;
import com.erp.erp_project.model.Plan;
import com.erp.erp_project.model.Work;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("dispatch")
public class DispatchController {
    private final DispatchMapper dispatchMapper;
    private final WorkMapper workMapper;
    private final PlanMapper planMapper;
    private final EquipMapper equipMapper;
    //展示
    @GetMapping("/list")
    public String list(){
        return "dispatch/list";
    }
    //展示
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult<PageInfo<Dispatch>> listAll(Dispatch dispatch,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        PageInfo<Dispatch> pageInfo=new PageInfo<>(dispatchMapper.list(dispatch));
        return new AjaxResult<PageInfo<Dispatch>>(pageInfo);
    }
    //作业编号编辑展示
    @GetMapping("work")
    @Permission("生产派工修改")
    public String work(Model model,String work){
        Equip equip=new Equip();
        List<Equip> workList=equipMapper.list(equip);
        model.addAttribute("workList",workList);
        model.addAttribute("work",workMapper.selectByNumber(work));
        return "dispatch/work";
    }
    //编辑
    @PostMapping("edit")
    @ResponseBody
    @Permission("生产派工修改")
    public AjaxResult<String> editWork(Work work){
        workMapper.update(work);
        return new AjaxResult<>("修改成功");
    }

    //添加
    @GetMapping("add")
    @Permission("生产派工添加")
    public String add(Model model){
        model.addAttribute("workList",workMapper.listAll());
        model.addAttribute("planList",planMapper.list(new Plan()));
        return "dispatch/add";
    }
    //添加
    @PostMapping("add")
    @ResponseBody
    @Permission("生产派工添加")
    public AjaxResult<String> add(Dispatch dispatch){
        dispatchMapper.add(dispatch);
        return new AjaxResult<>("添加成功");
    }
    //编辑
    //编辑
    @GetMapping("editDispatch")
    @Permission("生产派工编辑")
    public String edit(Model model,Dispatch dispatch){
        model.addAttribute("dispatch",dispatchMapper.selectById(dispatch.getId()));
        model.addAttribute("workList",workMapper.listAll());
        model.addAttribute("planList",planMapper.list(new Plan()));
        return "dispatch/edit";
    }
    @PostMapping("editDispatch")
    @ResponseBody
    @Permission("生产派工编辑")
    public AjaxResult<String> edit(Dispatch dispatch){
        dispatchMapper.update(dispatch);
        return new AjaxResult<>("修改成功");
    }
    //删除
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @Permission("生产派工删除")
    public AjaxResult<String> delete(@PathVariable Integer id) {
        dispatchMapper.delete(id);
        return new AjaxResult<String>("删除成功");
    }
}
